/* Copyright Andy Mitchell 2007. No warranties */

function Showhider(gtdinboxPage, id, options){
	this._gtdinboxPage = gtdinboxPage;
	this._id = id;
	this.doc = options.doc;
	this.eventObservers = new EventObservers(this);
	this.eventHandlers = new EventHandlers_Mutator(this);
	this.create(options);
	
	if( !this._gtdinboxPage.settings.showHiders ) this._gtdinboxPage.settings.showHiders = {};
	//if( this._gtdinboxPage.settings.showHiders.rememberOpenClose ){
		var show = this._gtdinboxPage.settings.showHiders[this._id];
		if( typeof show=="undefined" ) show = options.show;
		if( typeof options.overrideShow!="undefined" ) show = options.overrideShow;
		show = show? true:false;
		this.show(show);
	//}else{
	//	this.show(false);
	//}
}

Showhider.prototype.create = function(options){

try{
	if( !options.type ) options.type = "create";

	this.elTitleTxt = this.doc.createElement("SPAN");
	this.elTriangle = this.doc.createElement("IMG");
	switch( options.type ){
		case "create":
			this.el = this.doc.createElement("DIV");
				this.el.className = "showhider";

			if( options.elParent && options.elBefore ) options.elParent.insertBefore( this.el, options.elBefore );
			else if( options.elParent ) options.elParent.appendChild( this.el );
			
			this.setTitle(options.title);
			this.elTitle = this.doc.createElement("DIV");
			this.elContent = options.elContent;
			this.el.appendChild( this.elTitle );
			this.el.appendChild( this.elContent );
			break;
		case "attach":
			this.el = options.el;
			this.elTitle = GIPROTOTYPE.getElementsByClassName(this.el, options.titleClass)[0];
			this.setTitle( this.elTitle.innerHTML.replace(/<.+?>/g,"") );
			this.elTitle.innerHTML = "";
			this.elContent = this.elTitle.nextSibling;
			if( !this.elContent ){
				this.elContent = this.doc.createElement("DIV");
				this.el.appendChild( this.elContent );
			}
			break;
	}
	this.elTitle.appendChild( this.elTriangle );
	this.elTitle.appendChild( this.elTitleTxt );
		
	GIPROTOTYPE.addClassName(this.elTitle, "showhider-title");
	GIPROTOTYPE.addClassName(this.elContent, "showhider-content");
	
	this.elTitle.style.cursor = "pointer";
	if( !options.inline ){
		this.elTitleTxt.style.marginLeft = "3px;"
		this.elContent.style.paddingLeft = "14px";
	}
	this.eventObservers.observe( this.elTitle, "mousedown", this.mouseDownTitle, false );
	
	var elClear = this.doc.createElement("DIV");
		elClear.style.clear = "both";
		elClear.style.height = "1px";
	this.elTitle.appendChild( elClear );
}catch(e){
	if( GTDDEBUGMODE ) alert("Showhider create\n" + e.toString());
}

}

Showhider.prototype.setTitle = function(t){
	this.elTitleTxt.innerHTML = t;
};

Showhider.prototype.show = function(v){
	if( typeof v=="undefined" ){
		return this.elContent.style.display=="";
	}
	if( v && !this._disabled ){
		this.elTriangle.src = "chrome://gtdinbox/content/opentriangle.gif";
		GIPROTOTYPE.addClassName(this.el, "showhider-open");
		this.elContent.style.display = "";
		this.eventHandlers.broadcast("show");
		this.eventHandlers.broadcast("toggle");
		this._gtdinboxPage.settings.showHiders[this._id] = true;
	}else{
		GIPROTOTYPE.removeClassName(this.el, "showhider-open");
		this.elTriangle.src = "chrome://gtdinbox/content/triangle.gif";
		this.elContent.style.display = "none";
		this.eventHandlers.broadcast("hide");
		this.eventHandlers.broadcast("toggle");
		this._gtdinboxPage.settings.showHiders[this._id] = false;
	}
	this._gtdinboxPage.saveSettings();
};

Showhider.prototype.enabled = function(v){
	if( typeof v=="undefined" ) return !this._disabled;
	if( v ){
		GIPROTOTYPE.removeClassName(this.elTitle, "disabled");
		this._disabled = false;
	}else{
		GIPROTOTYPE.addClassName(this.elTitle, "disabled");
		this.show(false);
		this._disabled = true;
	}
};

Showhider.prototype.mouseDownTitle = function(event){
	this.toggle();
};
Showhider.prototype.toggle = function(){
	this.show( !this.show() );
};

Showhider.prototype.destroy = function(){
	this.eventObservers.destroy();
	this.eventHandlers.destroy();
	this.doc = null;
	this._gtdinboxPage = null;
}